﻿using System;
using Plugins;
using Utils.Wpf;

namespace PluginSample2
{
	internal class StrategyControlVM : ViewModelValidatedBase
	{
		#region Properties

		public StrategyControlData Data { get; private set; }

		public ExecutionDirection Mode
		{
			get
			{
				return this.Data.ExecutionDirection;
			}
			set
			{
				if (this.Data.ExecutionDirection != value)
				{
					this.Data.ExecutionDirection = value;
					base.OnPropertyChanged("ExecutionDirection");
				}
			}
		}

		public BarSource BarSource
		{
			get
			{
				return this.Data.BarSource;
			}
			set
			{
				if (this.Data.BarSource != value)
				{
					this.Data.BarSource = value;
					base.OnPropertyChanged("BarSource");
				}
			}
		}

		public int BarQuantity
		{
			get
			{
				return this.Data.BarQuantity;
			}
			set
			{
				if (this.Data.BarQuantity != value)
				{
					this.Data.BarQuantity = value;
					base.OnPropertyChanged("BarQuantity");
				}
			}
		}

		public int BarDuration
		{
			get
			{
				return (int)this.Data.BarDuration.TotalMinutes;
			}
			set
			{
				if ((int)this.Data.BarDuration.TotalMinutes != value)
				{
					this.Data.BarDuration = new TimeSpan(value * TimeSpan.TicksPerMillisecond * 60000);
					base.OnPropertyChanged("BarDuration");
				}
			}
		}

		public bool InvertLogic
		{
			get
			{
				return this.Data.InvertLogic;
			}
			set
			{
				if (this.Data.InvertLogic != value)
				{
					this.Data.InvertLogic = value;
					base.OnPropertyChanged("InvertLogic");
				}
			}
		}

		public bool RequirePureBars
		{
			get
			{
				return this.Data.RequirePureBars;
			}
			set
			{
				if (this.Data.RequirePureBars != value)
				{
					this.Data.RequirePureBars = value;
					base.OnPropertyChanged("RequirePureBars");
				}
			}
		}

		public int QuantityToBuy
		{
			get
			{
				return this.Data.QuantityToBuy;
			}
			set
			{
				if (this.Data.QuantityToBuy != value)
				{
					this.Data.QuantityToBuy = value;
					base.OnPropertyChanged("QuantityToBuy");
				}
			}
		}

		#endregion

		#region Public Methods

		public StrategyControlVM()
		{
			this.Data = new StrategyControlData();
			this.Data.PropertyChanged += Data_PropertyChanged; // Local object, do not need dispose
		}

		protected override string ValidateField(string fieldName)
		{
			string result = null;

			if (fieldName == "BarDuration")
			{
				if (this.BarDuration < 1 || this.BarDuration > 1440)
				{
					base.ValidatedErrors[fieldName] = true;
					result = "The duration cannot be smaller than 0 or greater than 24 hours";
				}
			}
			else if (fieldName == "BarQuantity")
			{
				if (this.BarQuantity < 1)
				{
					base.ValidatedErrors[fieldName] = true;
					result = "The quantity of bars to operate cannot be smaller than 1";
				}
			}
			else if (fieldName == "QuantityToBuy")
			{
				if (this.QuantityToBuy < 1)
				{
					base.ValidatedErrors[fieldName] = true;
					result = "The quantity to buy has to be greater than 0";
				}
			}

			return result;
		}

		#endregion

		#region Event Handlers

		private void Data_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
		{
			base.OnPropertyChanged(e.PropertyName);
		}

		#endregion
	}
}
